<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<script src="js/myajax.js" charset="utf-8"></script>
    <input id="search" type="text" placeholder="请输入要搜索的内容">
    <script>
      var oSearch = document.querySelector("#search");
      oSearch.onkeyup = function(event) {
        if (event.keyCode === 13) {
          location.href = 'search.html?search_text='+this.value;
        }
      }
    </script>
    
      <main>
    <ul id="hot-goods">
    </ul>
  </main>
  <script src="js/myajax.js" charset="utf-8"></script>
  <script>
    var search_text = getQueryString('search_text');
    //name abc
    //123
    var oGoods = document.querySelector('#hot-goods');
    myajax.get('http://h6.duchengjiu.top/shop/api_goods.php',
    {search_text: search_text},
    function(err,responseText){
      var json = JSON.parse(responseText);
      console.log(json);
      var data = json.data;
//      如果没数据要给出提示
      if (data.length === 0) {
        oGoods.innerHTML = "未搜索到商品, <span id='second'>5</span>秒后跳回首页";
        var oSecond = oGoods.querySelector('#second');
        var timer = setInterval(() => {
          var second = parseInt(oSecond.innerText);
          oSecond.innerText = --second;
        }, 1000);
        setTimeout(() => {
          clearInterval(timer);
          location.href = 'http://127.0.0.1:8020/%E9%A1%B9%E7%9B%AE/shop/index.html';
        }, 5000);
        return;
      }
      for (var i = 0; i < data.length; i++) {
        var obj = data[i];
        oGoods.innerHTML += `<li class="goods"><a href="goods.html?goods_id=${obj.goods_id}">
          <div><img src="${obj.goods_thumb}" /></div>
          <div>${obj.goods_name}</div>
          <div>${obj.price}</div></a>
        </li>`;
      }
    });
    </script>
	</body>
</html>
